/**
 * Created by wangbin on 2017/11/22.
 */
$(function() {
    //var ctx = "";//+window.location.pathname.split("/")[1];
    var url = {
        listUndo: ctx+"/incomeAcct/listUndoList",
        genCheckRecord:ctx+"/incomeAcct/genCheckRecord",
        cancelRecord: ctx+"/incomeAcct/cancel",
        appendRecord:ctx+"/incomeAcct/append",
        listRecord:ctx+"/incomeAcct/getCheckList",
        listItems:ctx+"/incomeAcct/listItems",
        invalid:ctx+"/incomeAcct/invalid",
        export:ctx+"/incomeAcct/export"
    };
    var initQuery = function () {
        qryLeft();
        qryCheckList();
    };
    var qryLeft = function () {
        var param = $("#qry-undo-form").serializeObject();
        $(top.callService("post",url.listUndo,param,function (data) {
        
            $("#simple-table tbody").html(data);
        },null,"html"));
    };
    var qryCheckList = function () {
        var param = $("#form_checkList").serialize();
        $(top.callService("post",url.listRecord,param,function (data) {
            $("#simple-table-income tbody").html(data);

            $(".js_btn_qry_check_detail").unbind("click").click(function (evt) {
               qryCheckDetail($(evt.target).attr("val"));
            });
        },null, "html"));
    };
    var qryCheckDetail = function (checkId) {
        var param = {};
        var t = $("#simple-table-income").find("input[name='ids']:checked").first();
        param.checkId = checkId || t.val() || "";

        if (param.checkId.length<1){
            return;
        }
        $(top.callService("post",url.listItems,param,function (data) {
            $("#check-income-detail tbody").html(data);
        },null, "html"));
    };

    var init = function() {
        XCF.listener();
        $("input[name='page.showCount']").val(XCF.global.pageSize);
        /*$(".js_selectAll").click(function (evt) {
            var t = $(evt.target).attr("checked");
            var items = $(evt.target).parent().parent().parent().parent().
                parent().find("input[type='checkbox']");
            if (t =="checked"){
                $.each(items,function (idx,item) {
                   $(item).attr("checked",t);
                });
            }else {
                $.each(items,function (idx,item) {
                    $(item).removeAttr("checked");
                });
            }
        });*/
        $(".js_btn_append").click(function () {
            var ids = "";
            var checkId= "";
            var t = $("#simple-table-income").find("input[name='ids']:checked").first();
            checkId = t.val();
            if (!checkId ||checkId.length<1){
                alert("请选择对账单号");
                return;
            }
            $.each($("#simple-table").find("input[name='ids']:checked"),
                function (idx,item) {
                ids = ids.concat(",").concat(item.id);
            });
            if (ids.length<1){
                alert("请选择未对账明细");
                return;
            }
            var param = {};
            param.checkId = checkId;
            param.undoList = ids.substring(1);
            $(top.callService("post",url.appendRecord,param,function(data){
                initQuery();
            }));
        });

        $(".js_btn_gen").click(function () {
            var ids = "";
            var dep = "";
            $.each($("#simple-table").find("input[name='ids']:checked"),function (idx,item) {
                ids = ids.concat(",").concat(item.id);
                if (idx<1){
                    dep = item.alt;
                }
            });
            var ipts = $(":checkbox:checked").parents("tr").find("input:text");  
         
            var checkCode="";
            var checkDept="";//对帐单位
           
            var total = {};            
            var totRmb={};
           
            var tormb=0.0;
            var tousd=0.0;
            var eqtRmb_=0.0;
            var eqtRmb=0.0;
            var array = $(".table tbody input[type=checkbox]:checked").map(function () {
//             billCode = $.trim($(this).closest("tr").find("td:eq(1)").text());
//             ladingBillCode = $.trim($(this).closest("tr").find("td:eq(2)").text());
//             settlementUnit = $.trim($(this).closest("tr").find("td:eq(3)").text());
//             costName = $.trim($(this).closest("tr").find("td:eq(4)").text());
//             taxPrice = $.trim($(this).closest("tr").find("td:eq(5)").text());
//             containerNum = $.trim($(this).closest("tr").find("td:eq(6)").text());
               
               currency = $.trim($(this).closest("tr").find("td:eq(8)").text());               
               totalPrice = $.trim($(this).closest("tr").find("td:eq(7)").text()); 
               equivalentRmb = $.trim($(this).closest("tr").find("td:eq(10)").text());
              
               
               if(currency=="RMB"){
            	   tormb+= parseFloat(totalPrice); 
            	   eqtRmb_+=parseFloat(equivalentRmb);
               }else{
            	   tousd += parseFloat(totalPrice); 
            	   eqtRmb+=parseFloat(equivalentRmb);
            	   
               }
            }).get();
            total["RMB"]=tormb.toFixed(2);
            total["USD"]=tousd.toFixed(2);
            total["EQTRMB_"]=eqtRmb_.toFixed(2);
            total["EQTRMB"]=eqtRmb.toFixed(2);
            
           
            if (ids.length<1){
                alert("请选择未对账明细");
                return;
            }
            var param = {};
            param.depart = dep;
            param.totalPrice=JSON.stringify(total);                     
            param.undoList = ids.substring(1);
            $(top.callService("post",url.genCheckRecord,param,function(data){
                initQuery();
            }));
        });

        $(".js_btn_invalid_items").click(function () {
            var ids = "";
            var checkId= "";
            $.each($("#check-income-detail").find("input[name='ids']:checked"),
                function (idx,item) {
                    ids = ids.concat(",").concat(item.id);
                    checkId = item.alt;
                });
            if(ids.length<1){
                alert("选择要操作的数据！")
                return;
            }
            var param = {};
            param.checkId = checkId;//$("#checkId").val();
            param.undoList = ids.substring(1);
            $(top.callService("post",url.invalid,param,function(data){
                qryCheckDetail();
                initQuery();
            }));
        });

        $(".js_btn_cancel_check").click(function () {
            var ids = "";
            $.each($("#simple-table-income").find("input[name='ids']:checked"),function (idx,item) {
                ids = ids.concat(",").concat(item.id);
            });
            if(ids.length<1){
                alert("请选择对账单号！")
                return;
            }
            var param = {};
            param.id = ids.substring(1);//$("#checkId").val();
            //param.undoList = ids.substring(1);
            $(top.callService("post",url.cancelRecord,param,function(data){
                qryCheckDetail();
                initQuery();
            }));
        });
        //申请开票
        $(".js_btn_gen_ticket").click(function () {
        	
       	 var checkId= "";
    
            var t = $("#simple-table-income").find("input[name='ids']:checked");
            if (!t){
                return;
            }
            if(t.length>1){
           	alert("温馨提示,您不能同时选择多行！");
           	return;
           	 
            }
           

         
            var ipts = $(":checkbox:checked").parents("tr").find("input:text");  
            var checkId="";
            var checkCode="";
            var checkDept="";//对帐单位
            var checkDeptVal="";
            var checkAmount="";
            var rmb=0;
            var usd=0;
            var array = $(".table tbody input[type=checkbox]:checked").map(function () {
            checkId = $.trim($(this).closest("tr").find("td:eq(1)").text());
           	checkCode = $.trim($(this).closest("tr").find("td:eq(2)").text());
           	checkDept = $.trim($(this).closest("tr").find("td:eq(3)").text());
            
            checkAmount = $.trim($(this).closest("tr").find("td:eq(6)").text());
            checkAmount=checkAmount.replace(/(^\s*)|(\s*$)/g,"");
         
           	var arr = checkAmount.split(",");           	
           	rmb = arr[0].split(":")[1].replace(/(^\s*)|(\s*$)/g,"");           	
           	usd = arr[1].split(":")[1].replace(/(^\s*)|(\s*$)/g,"");           
           	
            }).get();
            t = $(t).first();
            checkId = t.val();
            
            if (!checkId ||checkId.length<1){
                alert("请选择对账单号");
                return;
            }
            
           var diag = new top.Dialog();
           diag.Drag = true;
           diag.Title = "开票申请-(对账单号:"+checkCode+")";
           diag.URL = ctx+'/incomeAcct/invoiceApp?checkId='+checkId+"&checkCode="+checkCode+"&checkDept="+checkDept+"&rmb="+rmb+"&usd="+usd;
           diag.Width = 1150;
           diag.Height = 520;
           diag.CancelEvent = function () { //关闭事件
              
               diag.close();
           };
           diag.show();
       });

        /*导出*/
        /*$(".js_btn_export_01").click(function () {

        });
        $(".js_btn_exp_check").click(function () {

        });*/

        $(".div-left-search").css("display","none");
        /*列表查询*/
        $(".js_btn_left_search").click(function () {
            $(".div-left-search").css("display","block");
        }) ;
        $(".js_left_query").click(function () {
            $(".div-left-search").css("display","none");
            qryLeft();
        });
        $(".js_btn_qry_check").click(function () {
            qryCheckList();
        });
        $(".js_btn_qry_check_detail").click(function () {
            qryCheckDetail();
        });

        initQuery();
    };

    init();
});